/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.MainApplet;
import net.worlds.Util;


public class PresetsFrame extends EStickyFrame {
    private EButtonGroup buttonGroup = new EButtonGroup();
    private EButton[] modelButtons = new EButton[]{};
    private String categoriesResult;
    private String modelsResult;
    private String modelResult;
    public PresetsFrame(EComponent parent)
    {
        super(parent, -1, 80, 5, 230, 60, 0);
        loadCategories();
        setBackgroundColor(new Color(192, 192, 192));
        setBorderColor(new Color(0, 0, 0, 0));
    }
    private void loadCategories()
    {
        Util.performRequestWithCallback(
        new Runnable()
        {

            public void run() {
                categoriesResult = Util.httpGet("http://"+Main.siteURL+"/game/models/categories.txt");
            }

        },
        new Runnable()
        {
            public void run() {
                String[] categories = categoriesResult.split("\n");
                int x = 0;

                for(final String c : categories)
                {
                    EToggleButton b = new EToggleButton(c);
                    b.setButtonGroup(buttonGroup);
                    b.setColor(new Color(192, 192, 192));
                    b.setTextColor(new Color(64, 64, 64));
                    b.setHeight(16);
                    b.setX(x);
                    b.setY(0);
                    b.setActionListener(new EActionListener()
                    {
                        @Override
                        public void actionPerformed() {
                            loadModels(c);
                        }
                    });
                    addChild(1, b);
                    x += b.getWidth();
                }
                loadModels(categories[0]);
            }
        });
    }
    private void loadModels(final String category)
    {
        Util.performRequestWithCallback(
        new Runnable()
        {

            public void run() {
                try {
                    modelsResult = Util.httpGet("http://"+Main.siteURL+"/game/models/models.php?cat=" + URLEncoder.encode(category, "UTF-8"));
                } catch (UnsupportedEncodingException ex) {
                    ex.printStackTrace();
                }
            }

        },
        new Runnable()
        {
            public void run() {
                String[] rows = modelsResult.split(";");
                int i = 0;
                for(EButton b : modelButtons)
                {
                    removeChild(b);
                }
                modelButtons = new EButton[rows.length - 1];
                int x = 0;
                for(String row : rows)
                {
                    String[] data = row.split(",");
                    if(data.length < 3)
                        continue;
                    final int id = Integer.parseInt(data[0]);
                    String name = data[1];
                    String desc = data[2];
                    EButton b = new EButton(name);
                    b.setWidth(100);
                    b.setHeight(80 - 16);
                    b.setColor(new Color(150, 150, 150));
                    b.setTooltip(desc);
                    b.setX(x);
                    b.setY(16);
                    b.setActionListener(new EActionListener()
                    {
                        @Override
                        public void actionPerformed() {
                            insertModel(id);
                        }
                    });
                    addChild(1, b);
                    modelButtons[i] = b;
                    i++;
                    x += 100;
                }
            }
        });
    }
    private void insertModel(final int id)
    {
        Util.performRequestWithCallback(
        new Runnable()
        {

            public void run() {
                modelResult = Util.httpGet("http://"+Main.siteURL+"/game/models/model.php?id=" + id);
            }

        },
        new Runnable()
        {
            public void run() {
                try {
                    String model = Util.base64Decode(modelResult);
                    Main.loadModel(model);
                } catch (WorldsException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}
